<template>
	<view>
		<view class="" style="">
			<scroll-view scroll-x class="bg-white nav text-center fixed">
				<view class="cu-item" :class="index==TabCur?'text-blue cur':''" v-for="(item,index) in tabNav"
					:key="index" @tap="tabSelect" :data-id="index">
					{{tabNav[index]}}
				</view>
			</scroll-view>
			<block v-if="TabCur==0">
				<view v-for="(item, index) in jinhuoList" :key="index" style="margin-top: 10rpx; margin-bottom: 20rpx;"> 
					<view class="myfirst" style="margin-top: 20rpx;">
						<navigator class="" style="display: flex; margin-bottom: 10px;"
							:url="'/pages/goods/detail/detail?goods_id=' + item.goods_id">
							<view class="mypic">
								<image
									:src="item.pic_cover_small == '' ? $util.img('upload/uniapp/default_goods.png') : $util.img(item.pic_cover_small)"
									mode="aspectFit"></image>
							</view>
							<view class="mycontent" style="">
								<text class="mydesc">{{ item.goods_name }}</text>
								<view class="myoperation">
									<text class="" style="color: #FF0036 !important;">¥{{ item.promotion_price }}</text>
									<!-- <text class="" style="color: #898989 !important;">销量：{{ item.sales }}</text> -->
								</view>
							</view>
						</navigator>
						<view class="" style="display: flex; align-items: center; margin: auto 0;">
							<view class="uni-numbox">
								<view class="uni-numbox__minus" @click="jian(index)">
									-</view>
								<text
									style="display: flex;align-items: center;width: 50rpx; text-align: center; justify-content: center;">
									{{jinhuoList[index].min_buy}}
								</text>
								<view class="uni-numbox__plus" @click="add(index)">+</view>
							</view>
						</view>
					</view>

				</view>
			</block>
			<block v-if="TabCur==1">
				<view v-for="(item, index) in weiList" :key="index" style="margin-top: 10rpx; margin-bottom: 20rpx;">
					<view class="myfirst" style="">
						<navigator class="" style="display: flex; margin-bottom: 10px;"
							:url="'/pages/goods/detail/detail?goods_id=' + item.goods_id">
							<view class="mypic">
								<image
									:src="item.pic_cover_small == '' ? $util.img('upload/uniapp/default_goods.png') : $util.img(item.pic_cover_small)"
									mode="aspectFit"></image>
							</view>
							<view class="mycontent" style="">
								<text class="mydesc">{{ item.goods_name }}</text>
								<view class="myoperation">
									<text class="" style="color: #FF0036 !important;">¥{{ item.promotion_price }}</text>
									<!-- <text class="" style="color: #898989 !important;">销量：{{ item.sales }}</text> -->
								</view>
							</view>
						</navigator>
						<view class="" style="display: flex; align-items: center; margin: auto 0;">
							<view class="uni-numbox">
								<view class="uni-numbox__minus" @click="weijian(index)">
									-</view>
								<text
									style="display: flex;align-items: center;width: 50rpx; text-align: center; justify-content: center;">
									{{weiList[index].min_buy}}
								</text>
								<view class="uni-numbox__plus" @click="weiadd(index)">+</view>
							</view>
						</view>

					</view>
				</view>
			</block>
		</view>
	</view>
</template>
<style lang="scss">
	@import 'static/css/goods/detail.scss';
</style>
<script>
	import uniNumberBox from '@/components/uni-number-box/uni-number-box.vue';
	import http from 'common/js/http.js';
	import orderAction from '../public/js/orderAction.js';
	import detail from '../../../static/js/goods/detail.js'
	export default {
		components: {
			uniNumberBox,
		},
		mixins: [http, detail],
		mixins: [http, orderAction],
		data() {
			return {
				CustomBar: this.CustomBar,
				TabCur: 0,
				tabNav: ['已购买', '未购买'],
				jinhuoList: '', //已购买
				weiList: '', //未购买
			};
		},
		async onShow() {
			this.getjinhuoList();
		},
		methods: {

			// 未购买
			weijian(index) {
				// console.log('减少',index)
				console.log('减少', this.weiList[index].min_buy)
				if (this.weiList[index].min_buy < 2) {
					console.log('不能再小了')
					uni.showToast({
						title: '无法减少',
						icon: 'success',
						duration: 500
					})
				}
				// 否则加 -1
				else {
					this.weiList[index].min_buy -= 1
					var cart_detail = {
						goods_id:this.weiList[index].goods_id,
						goods_name:this.weiList[index].goods_name,
						sku_id: this.weiList[index].sku_id,
						price: this.weiList[index].price,
						picture_id: this.weiList[index].picture,
						count: -this.weiList[index].min_buy,
						shop_name: ''
					};
					this.sendRequest({
						url: "System.Goods.addCarts",
						data: {
						"cart_detail": JSON.stringify(cart_detail)
						},
						success: res => {
							// console.log('可能成功了', res)
						}
					});
				}
			},
			weiadd(index) {
				console.log('增加', index)
				this.weiList[index].min_buy += 1
				
				var cart_detail = {
					goods_id:this.weiList[index].goods_id,
					goods_name:this.weiList[index].goods_name,
					sku_id: this.weiList[index].sku_id,
					price: this.weiList[index].price,
					picture_id: this.weiList[index].picture,
					count: this.weiList[index].min_buy,
					shop_name: ''
				};
				this.sendRequest({
					url: "System.Goods.addCarts",
					data: {
						"cart_detail": JSON.stringify(cart_detail)
					},
					success: res => {
						if(res.message == 'success'){
							uni.showToast({
								title:'添加购物车成功',
								icon:'success',
								duration:3000
							})
							console.log('可能成功了', res)
						}
						// console.log('可能成功了', res)
					}
				});
			},

			//已购买
			jian(index) {
				// 如果数量<0的话无法减少
				console.log('减少', this.jinhuoList[index].min_buy)
				if (this.jinhuoList[index].min_buy < 2) {
					console.log('不能再小了')
					uni.showToast({
						title: '无法减少',
						icon: 'success',
						duration: 500
					})
				}
				// 否则加1
				else {
					this.jinhuoList[index].brand_id -= 1
					var cart_detail = {
						goods_id:this.jinhuoList[index].goods_id,
						goods_name:this.jinhuoList[index].goods_name,
						sku_id: this.jinhuoList[index].sku_id,
						price: this.jinhuoList[index].price,
						picture_id: this.jinhuoList[index].picture,
						count: -this.jinhuoList[index].min_buy,
						shop_name: ''
					};
					this.sendRequest({
						url: "System.Goods.addCarts",
						data: {
							"cart_detail": JSON.stringify(cart_detail)
						},
						success: res => {
							
								uni.showToast({
									title:'减少成功',
									icon:'success',
									duration:3000
								})
								console.log('res==', res)
							
							
						}
					});
				}
			},
			add(index) {
				console.log('增加', index)
				this.jinhuoList[index].brand_id += 1
				console.log('增加this.brand_id', this.jinhuoList[index].brand_id)

				//如果数量brand_id小于等于0时，返回值出现错误
				if (this.jinhuoList[index].brand_id < 0) {
					console.log('返回值出现错误')
					this.jinhuoList[index].brand_id=0
				} else {
					console.log('this.jinhuoList[index].skuId=',this.jinhuoList[index].sku_id)
					var cart_detail = {
						goods_id:this.jinhuoList[index].goods_id,
						goods_name:this.jinhuoList[index].goods_name,
						sku_id: this.jinhuoList[index].sku_id,
						price: this.jinhuoList[index].price,
						picture_id: this.jinhuoList[index].picture,
						count: this.jinhuoList[index].min_buy,
						shop_name: ''
					};
					
					this.sendRequest({
						url: "System.Goods.addCarts",
						data: {
							"cart_detail": JSON.stringify(cart_detail)
						},
						success: res => {
							if(res.message == 'success'){
								uni.showToast({
									title:'添加购物车成功',
									icon:'success',
									duration:3000
								})
								console.log('可能成功了', res)
							}
							
						}
					});
				}
			},
			//已购买结束

			//切换事件
			tabSelect(e) {
				this.TabCur = e.currentTarget.dataset.id;
				this.scrollLeft = (e.currentTarget.dataset.id - 1) * 60
			},

			// 获取进货单中的商品
			getjinhuoList() {
				var that = this;
				this.sendRequest({
					url: 'System.Order.orderGoods',
					data: {},
					success: res => {
						// console.log('System.Order.orderGoods  res===============', res)
						this.jinhuoList = res.data.good //已购买
						this.weiList = res.data.goods //未购买
						console.log('返回商品==',res)
						console.log('返回已购买的商品==', this.jinhuoList)
						// for (let i = 0; i < this.jinhuoList.length; i++) {
						// 	this.jinhuo[i]
						// }
						console.log('返回未购买的商品==', this.weiList)
					}
				});
			},


		},

	}
</script>
<style lang="scss">

</style>
<style lang="scss">
	@import '../../goods/public/css/category.scss';
	@import '../../../static/css/main.css';
	// @import '../../../static/css/goods/detail.scss';
	@import '../public/css/list.scss';

	.mycontent {
		flex: 1;
		margin-left: 10px;
		display: flex;
		flex-direction: column;
		justify-content: space-around;
	}

	.myoperation {
		display: flex;
		justify-content: space-between;
	}

	.mypic {
		margin-right: 30px;
	}

	.mypic image {
		// margin-right: 10px;
		width: 88px;
		height: 88px;
	}

	.myfirst {
		display: flex;
		margin-bottom: 10px;
		justify-content: space-between;
		background-color: #FFFFFF;
		width: 700rpx;
		margin: 0 auto;
		padding: 20rpx;
	}

	page {
		background-color: #f8f8f8;
		padding-top: 45px;
	}





	.uni-numbox {
		display: inline-flex;
		flex-direction: row;
		justify-content: flex-start;
		height: 70rpx;
		position: relative;
	}

	.uni-numbox.small {
		height: 50rpx;
	}

	.uni-numbox:after {
		content: '';
		position: absolute;
		transform-origin: center;
		box-sizing: border-box;
		pointer-events: none;
		top: -50%;
		left: -50%;
		right: -50%;
		bottom: -50%;
		border: 1px solid #e5e5e5;
		border-radius: 12rpx;
		transform: scale(0.5);
	}

	.uni-numbox__minus,
	.uni-numbox__plus {
		margin: 0;
		background-color: #f8f8f8;
		width: 70rpx;
		font-size: 40rpx;
		height: 100%;
		line-height: 70rpx;
		text-align: center;
		display: inline-flex;
		align-items: center;
		justify-content: center;
		color: #333;
		position: relative;
	}

	.uni-numbox__value {
		position: relative;
		background-color: #fff;
		width: 80rpx;
		height: 100%;
		text-align: center;
		padding: 0;
	}

	.uni-numbox__minus.small,
	.uni-numbox__plus.small {
		width: 50rpx;
		line-height: 50rpx;
	}

	.uni-numbox__value.small {
		width: 60rpx;
		font-size: 28rpx;
	}

	.uni-numbox__value:after {
		content: '';
		position: absolute;
		transform-origin: center;
		box-sizing: border-box;
		pointer-events: none;
		top: -50%;
		left: -50%;
		right: -50%;
		bottom: -50%;
		border-style: solid;
		border-color: #e5e5e5;
		border-left-width: 1px;
		border-right-width: 1px;
		border-top-width: 0;
		border-bottom-width: 0;
		transform: scale(0.5);
	}

	.uni-numbox--disabled {
		color: silver;
	}
</style>
